import { GameEngine } from "./gameEngine";
export class Enemys {
  public el: HTMLDivElement;
  private timer: any;
  private boomTimer: any;
  private id: number;
  constructor() {
    this.id = Math.floor(Math.random() * 10000);
    this.el = document.createElement("div");
    GameEngine.el.appendChild(this.el);
    this.el.className = "enemy-small";
    this.el.style.top = "0px";
    this.el.style.left = parseInt(Math.random() * (GameEngine.el.offsetWidth - this.el.offsetWidth) + "") + 'px';
    this.move();
    GameEngine.enemys[Number(this.id)] = this //this是实例对象
  }
  move() {
    let mY = parseInt((Math.random() * 21) + "") + 1
    this.timer = setInterval(() => {
      this.el.style.top = this.el.offsetTop + mY + "px";
      if (this.el.offsetTop > document.documentElement.clientHeight) {
        clearInterval(this.timer);
        this.el.remove();
        GameEngine.enemys.splice(this.id, 1);
      }
    }, 60)
  }
  boom() {
    let num = 0;
    let boomTimer = setInterval(() => {
      num++;
      this.el.style.backgroundImage = `url(./images/plane1_die${num}.png)`;
      if (num >= 3) {
        clearInterval(boomTimer);
        GameEngine.enemys.splice(this.id, 1);
        this.el.remove();
      }
    }, 60);
  }
}


